package xcmg.device.service.warehouse;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.DataDictionaryDO;
import xcmg.device.dao.entity.warehouse.BillTypeConfigDO;
import xcmg.device.dao.entity.warehouse.BillTypeDetailDO;
import xcmg.device.dao.mapper.DataDictionaryMapper;
import xcmg.device.dao.mapper.warehouse.BillTypeConfigMapper;
import xcmg.device.dao.mapper.warehouse.BillTypeDetailMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.basic.DictionaryRelationVO;
import xcmg.device.service.vo.warehouse.BillTypeConfigVO;
import xcmg.device.service.vo.warehouse.BillTypeDetailVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BillTypeConfigService {

    @Autowired
    private BillTypeConfigMapper billTypeConfigMapper;

    @Autowired
    private BillTypeDetailMapper billTypeDetailMapper;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private CommonService commonService;
    
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    /**
     * @Author: WangKe
     * @Description: 新增单据配置类型
     * @Date: 2018/1/29 0029
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(BillTypeConfigVO billTypeConfigVO, String userId, String orgId) throws Exception{
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        billTypeConfigVO.setId(textResult.getValue());
        billTypeConfigVO.setOrgId(orgId);
        billTypeConfigVO.setCreateId(userId);
        billTypeConfigVO.setCreateName(commonService.getUserName(userId));
        billTypeConfigVO.setCreateDate(new Date());

        BillTypeConfigDO billTypeConfigDO = new BillTypeConfigDO();
        BeanUtils.copyProperties(billTypeConfigVO, billTypeConfigDO);
        for (BillTypeDetailVO detail: billTypeConfigVO.getDetailList()
             ) {
            ActionResult<String> detailText = fastGenClient.textGuid();
            if (detailText.getCode() != ErrorCode.Success.getCode())
            {
                continue;
            }
            if(StringUtil.isNullOrSpace(detail.getInnerOrder()))
            {
                continue;
            }
            BillTypeDetailDO billTypeDetailDO=new BillTypeDetailDO();
            billTypeDetailDO.setId(detailText.getValue());
            billTypeDetailDO.setTypeId(billTypeConfigDO.getId());
            billTypeDetailDO.setInnerOrder(detail.getInnerOrder());
            billTypeDetailDO.setInnerOrderNote(detail.getInnerOrderNote());
            billTypeDetailMapper.insert(billTypeDetailDO);
        }
        return billTypeConfigMapper.insert(billTypeConfigDO)>0? ErrorCode.Success: ErrorCode.Failure;
    }

    /**
     * @Author: WangKe
     * @Description: 根据主键批量删除单据类型
     * @Date: 2018/1/29 0029
     */
    public ErrorCode remove(List<String> ids){
        boolean success = billTypeConfigMapper.remove(ids)>0;
        success=billTypeDetailMapper.remove(ids)>0;
        return success? ErrorCode.Success: ErrorCode.WarehouseIdError;
    }

    /**
     * @Author: WangKe
     * @Description: 查询单据类型列表
     * @Date: 2018/1/29 0029
     */
    public PageCommonVO list(SearchCommonVO<BillTypeConfigVO> condition) {
        PageCommonVO<BillTypeConfigVO> pageCommonVO = new PageCommonVO();
        PageHelperPlus.orderBy("CREATE_DATE desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<BillTypeConfigDO> doList = billTypeConfigMapper.list(condition.getFilters());
        List<BillTypeConfigVO> voList = new ArrayList<>();
        for (BillTypeConfigDO entity : doList) {
            BillTypeConfigVO model = new BillTypeConfigVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }
    
    public List<BillTypeConfigVO> listAllOutTypes(BillTypeConfigVO billTypeConfigVO) {
        List<BillTypeConfigVO> voList = new ArrayList<BillTypeConfigVO>();
        DictionaryRelationVO condition = new DictionaryRelationVO();
        condition.setParentCode("CKLX");
        List<DictionaryRelationVO> dictionaryRelationVOs = dataDictionaryMapper.listAll(condition);
        if (CollectionUtils.isNotEmpty(dictionaryRelationVOs)) {
            for (DictionaryRelationVO dictionaryRelationVO : dictionaryRelationVOs) {
                BillTypeConfigVO vo = new BillTypeConfigVO();
                vo.setId(dictionaryRelationVO.getId());
                vo.setType(dictionaryRelationVO.getName());
                voList.add(vo);
            }
        }
        List<BillTypeConfigDO> doList = billTypeConfigMapper.list(billTypeConfigVO);
        if (CollectionUtils.isNotEmpty(doList)) {
            for (BillTypeConfigDO entity : doList) {
                BillTypeConfigVO model = new BillTypeConfigVO();
                BeanUtils.copyProperties(entity, model);
                voList.add(model);
            }
        }
        return voList;
    }

    /**
     * @Author: WangKe
     * @Description: 根据主键查询单据配置类型
     * @Date: 2018/1/29 0029
     */
    public BillTypeConfigVO findByPK(String id){
        BillTypeConfigDO entity = billTypeConfigMapper.selectByPrimaryKey(id);
        if(entity == null) {
            DataDictionaryDO dataDictionaryDO = dataDictionaryMapper.item(id);
            if (dataDictionaryDO == null) {
                return null;
            }
            BillTypeConfigDO billTypeConfigDO = new BillTypeConfigDO();
            billTypeConfigDO.setId(id);
            billTypeConfigDO.setType(dataDictionaryDO.getName());
            billTypeConfigDO.setIsSynsap(0);
            entity = billTypeConfigDO;
        }
        BillTypeConfigVO billTypeConfigVO = new BillTypeConfigVO();
        BeanUtils.copyProperties(entity, billTypeConfigVO);
        BillTypeDetailVO billTypeDetailVO=new BillTypeDetailVO();
        billTypeDetailVO.setTypeId(id);
        List<BillTypeDetailDO> billTypeDetailDOS=billTypeDetailMapper.list(billTypeDetailVO);
        List<BillTypeDetailVO> billTypeDetailVOS=new ArrayList<>();
        for (BillTypeDetailDO detailDO:billTypeDetailDOS
             ) {
            BillTypeDetailVO detailVO=new BillTypeDetailVO();
            BeanUtils.copyProperties(detailDO,detailVO);
            billTypeDetailVOS.add(detailVO);
        }
        billTypeConfigVO.setDetailList(billTypeDetailVOS);
        return billTypeConfigVO;
    }

    /**
     * @Author: WangKe
     * @Description: 更新记录
     * @Date: 2018/2/1 0001
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode update(BillTypeConfigVO vo) throws Exception{
        BillTypeConfigDO model = new BillTypeConfigDO();
        BeanUtils.copyProperties(vo, model);
        billTypeDetailMapper.deleteByPrimaryType(model.getId());
        for (BillTypeDetailVO detail: vo.getDetailList()
             ) {
            ActionResult<String> detailText = fastGenClient.textGuid();
            if (detailText.getCode() != ErrorCode.Success.getCode())
            {
                continue;
            }
            if(StringUtil.isNullOrSpace(detail.getInnerOrder()))
            {
                continue;
            }
            BillTypeDetailDO billTypeDetailDO=new BillTypeDetailDO();
            billTypeDetailDO.setId(detailText.getValue());
            billTypeDetailDO.setTypeId(model.getId());
            billTypeDetailDO.setInnerOrder(detail.getInnerOrder());
            billTypeDetailDO.setInnerOrderNote(detail.getInnerOrderNote());
            billTypeDetailMapper.insert(billTypeDetailDO);
        }
        return billTypeConfigMapper.updateByPrimaryKeySelective(model)>0? ErrorCode.Success: ErrorCode.WarehouseIdError;
    }

}
